package nbcp.web.info

import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import nbcp.base.comm.ApiResult
import nbcp.base.comm.JsonResult
import nbcp.base.extend.ConvertJson
import nbcp.base.extend.ConvertType
import nbcp.base.extend.Corp
import nbcp.comm.JsonpMapping
import nbcp.db.mongo.entity.SysUser
import nbcp.db.mongo.*
import nbcp.db.mongo.entity.HomeData
import nbcp.db.mongo.entity.ProductCard
import nbcp.xmz_corp_id
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest


/**
 * Created by udi on 17-4-1.
 */

@Api(description = "首页信息", tags = arrayOf("Home"))
@RestController
@JsonpMapping("/info")
class Home_Controller {
    @ApiOperation("设置首页信息")
    @JsonpMapping("/home/set")
    fun home_set(field: String, productId: String, request: HttpServletRequest): JsonResult {
        var product = mor.info.productInfo.queryById(productId)
                .where { it.corp.id match request.Corp.id }
                .toEntity();

        if (product == null) {
            return JsonResult("找不到商品")
        }
        var productCard = product.ConvertJson(ProductCard::class.java)
        productCard.logo = product.logo;

        mor.shop.homeData.update()
                .where { it.corpId match xmz_corp_id }
                .set { MongoColumnName(field) to productCard }
                .exec()

        return JsonResult();
    }

    @ApiOperation("设置首页信息")
    @JsonpMapping("/home/getData")
    fun home_get(): ApiResult<HomeData> {
        var ret = mor.shop.homeData.query()
                .where { it.corpId match xmz_corp_id }
                .toEntity()
        return ApiResult.of(ret);
    }

    @ApiOperation("修改我的信息")
    @JsonpMapping("/saveInfo")
    fun saveInfo(): JsonResult {
        return JsonResult();
    }
}